package com.example.minefieldkeepercloud;

import android.content.DialogInterface;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.minefieldkeepercloud.fragment.FiveFragment;
import com.example.minefieldkeepercloud.fragment.FourFragment;
import com.example.minefieldkeepercloud.fragment.HomeFragment;
import com.example.minefieldkeepercloud.fragment.MinerHarvestFragment;
import com.example.minefieldkeepercloud.fragment.ThreeFragment;
import com.example.minefieldkeepercloud.fragment.TwoFragment;
import com.example.minefieldkeepercloud.view.lazyviewpager.LazyFragmentPagerAdapter;
import com.example.minefieldkeepercloud.view.lazyviewpager.LazyViewPager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    @BindView(R.id.viewpager)
    LazyViewPager viewpager;
    @BindView(R.id.iv_homepage)
    ImageView iv_homepage;
    @BindView(R.id.iv_orePool)
    ImageView iv_orePool;
    @BindView(R.id.iv_set)
    ImageView iv_set;
    @BindView(R.id.iv_miner)
    ImageView iv_miner;
    @BindView(R.id.tv_homepage)
    TextView tv_homepage;
    @BindView(R.id.tv_orePool)
    TextView tv_orePool;
    @BindView(R.id.tv_miner)
    TextView tv_miner;
    @BindView(R.id.tv_set)
    TextView tv_set;

    private CustomLazyFragmentPagerAdapter customLazyFragmentPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initview();
    }

    /**
     * 初始化
     */
    private void initview() {
        customLazyFragmentPagerAdapter = new CustomLazyFragmentPagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(customLazyFragmentPagerAdapter);
    }

    /**
     * 底部类别点击事件处理
     *
     * @param view
     */
    @OnClick({R.id.ly_homepage, R.id.ly_orePool, R.id.ly_miner, R.id.ly_set})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ly_homepage:
                setBottomLabState(0);
                viewpager.setCurrentItem(0, false);
                break;
            case R.id.ly_orePool:
                setBottomLabState(1);
                viewpager.setCurrentItem(1, false);
                break;
            case R.id.ly_miner:

                setBottomLabState(2);
                viewpager.setCurrentItem(2, false);

                break;

            case R.id.ly_set://设置
                setBottomLabState(4);
                viewpager.setCurrentItem(4, false);

                break;

        }
    }

    /**
     * 点击的底部分类图标状态
     *
     * @param position
     */
    private void setBottomLabState(int position) {
        switch (position) {
            case 0:
                setTextAndImg(tv_homepage, iv_homepage, R.mipmap.kjrsll, true);
                setTextAndImg(tv_orePool, iv_orePool, R.mipmap.kjslh, false);
                setTextAndImg(tv_miner, iv_miner, R.mipmap.kjwhh, false);

                setTextAndImg(tv_set, iv_set, R.mipmap.wdh, false);
                break;
            case 1:
                setTextAndImg(tv_homepage, iv_homepage, R.mipmap.kjrslh, false);
                setTextAndImg(tv_orePool, iv_orePool, R.mipmap.kjsll, true);
                setTextAndImg(tv_miner, iv_miner, R.mipmap.kjwhh, false);

                setTextAndImg(tv_set, iv_set, R.mipmap.wdh, false);
                break;
            case 2:
                setTextAndImg(tv_homepage, iv_homepage, R.mipmap.kjrslh, false);
                setTextAndImg(tv_orePool, iv_orePool, R.mipmap.kjslh, false);
                setTextAndImg(tv_miner, iv_miner, R.mipmap.kjwhl, true);

                setTextAndImg(tv_set, iv_set, R.mipmap.wdh, false);
                break;

            case 4:
                setTextAndImg(tv_homepage, iv_homepage, R.mipmap.kjrslh, false);
                setTextAndImg(tv_orePool, iv_orePool, R.mipmap.kjslh, false);
                setTextAndImg(tv_miner, iv_miner, R.mipmap.kjwhh, false);

                setTextAndImg(tv_set, iv_set, R.mipmap.wdl, true);
                break;

        }
    }

    /**
     * 设置选择分类的样式
     *
     * @param tv
     * @param iv
     * @param res
     * @param isBlue
     */
    private void setTextAndImg(TextView tv, ImageView iv, int res, boolean isBlue) {
        iv.setImageResource(res);
        if (isBlue) {
            tv.setTextColor(ContextCompat.getColor(this, R.color.system_color));
        } else {
            tv.setTextColor(ContextCompat.getColor(this, R.color.black));
        }
    }


    private static class CustomLazyFragmentPagerAdapter extends LazyFragmentPagerAdapter {

        private CustomLazyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(ViewGroup container, int position) {
            if (position == 0) {
                return new HomeFragment();
            } else if (position == 1) {
                return new MinerHarvestFragment();
            } else if (position == 2) {
                return new ThreeFragment();
            } else if (position == 3) {
                return new FourFragment();
            } else if (position == 4) {
                return new FiveFragment();
            }
            return null;
        }

        @Override
        public int getCount() {
            return 5;
        }

    }

    /**
     * 返回键隐藏到后台
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            dialog();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /*退出对话框*/
    public void dialog() {
        final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("提示")
                .setMessage("确定要退出吗？")
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
//                        Intent intent = new Intent(Intent.ACTION_MAIN);
//                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                        intent.addCategory(Intent.CATEGORY_HOME);
//                        startActivity(intent);
                        finish();
                    }
                })
                .setNegativeButton("否", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        AlertDialog dialog_dialog = dialog.create();
        dialog_dialog.show();
    }


}
